import scrapy
import re#正则解析

class StocksSpider(scrapy.Spider):
    name = 'stocks'
    start_urls = ['http://quote.eastmoney.com/stocklist.html']

    def parse(self, response):
        for herf in response.css('a::attr(href)').extract():#a标签
            try:
                stock = re.findall(r"[s][hz]\d{6}",herf)[0]#re匹配股票代码
                url = 'https://gupiao.baidu.com/stock'+stock+'.html'#百度股票对应的链接信息
                yield scrapy.Request(url,callback=self.parse_stock)#callback为处理此url对应响应的处理函数
            except:
                continue

    def parse_stock(self,response):#此函数最终返回提取的信息给item——pipeline
        infoDict = {}
        stockInfo = response.css('.stock-bets')#提取
        name = stockInfo.css('.bets-name').extract()[0]#进一步检索并且提取相关字符串
        keyList = stockInfo.css('dt').extract()#提取键值（其他信息）
        valueList = stockInfo.css('dd').extract()
        for i in range(len(keyList)):#保存信息到字典
            key=re.findall(r'>.*</dt>', keyList[i])[1:-5]
            try:
                val=re.findall(r'\d+\.?.*<dd>', valueList[i])[0][0:-5]
            except:
                val='--'
            infoDict[key]=val
        infoDict.update(
            {'名称':re.findall('\s.*\(',name)[0].split()[0] + \
                    re.findall('\>*\<',name)[0][1:-1]}
        )#至此，此字典保存了提取的名称及相关信息
        yield infoDict





